﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VMG.Format;
using Portal.Core;
using bUser = Portal.Core.Customer.bUser;
using System.IO;
public partial class Controls_ChangedInfo : System.Web.UI.UserControl
{
    int Id_User = 1;
    protected void Page_Load(object sender, EventArgs e)
    {
        bUser.GetDetailCustomerForID(1);

        if (!IsPostBack)
        {
            lbl_username.Text = bUser.UserName;
            lbl_email.Text = bUser.Email;
            lbl_Gold.Text = bUser.Gold.ToString() + "&nbsp; Gold";
            txb_Funllname.Text = bUser.FullName;
            txb_PhoneNumber.Text = bUser.PhoneNumber;
            id_Avatar.Value = Informations.basepath + bUser.Avatar;
            //txb_Avatar.Text = bUser.Avatar;
            txb_Birthday.Text = bUser.BirthDay.ToString("dd/MM/yyyy");
        }
        Img_Avatar.ImageUrl = Informations.basepath + bUser.Avatar;
    }
    protected void cmd_Update_Click(object sender, EventArgs e)
    {
        string Megs = "<script language='javascript'>";
        DateTime BirthDay;
        string BirtDay_ = txb_Birthday.Text;
        BirthDay =  CheckInput.ConvertDateTime(BirtDay_.Replace("-", "/"));
        string Fullname = txb_Funllname.Text;
        string PhoneNumber = txb_PhoneNumber.Text;
        int Output = bUser.UpdateInfoCustomer(Id_User, Fullname, BirthDay, PhoneNumber, id_Avatar.Value);
        if (Output == -1)
        {
            Megs += "alert('Thay đổi thông tin không thành công, mời làm lại!');</script>";
        }
        else
        {
            Megs += "alert('Thay đổi thông tin thành công!');</script>";
        }
        Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "canhbao", Megs);

    }
    protected void cmd_UploadAvatar_Click(object sender, EventArgs e)
    {
        Random rd1 = new Random();
        string Avatar = string.Empty;
        string Megs = "<script language='javascript'>";
        string NameImg = DateTime.Now.ToString("MM/yy").Replace("-", "").Replace("/", "") + "_" + rd1.Next(100009, 999999).ToString();
        string baseImageLocation = Server.MapPath("Images\\Avatar\\");
        string fileExt = Path.GetExtension(uploadPicture.FileName).ToLower();
        string fileName = Path.GetFileName(uploadPicture.FileName);
        string PathImage = "";
        if (fileName != "")
        {
            if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".png" || fileExt == ".jpge")
            {
                PathImage = baseImageLocation + NameImg + fileExt;
                try
                {
                    uploadPicture.PostedFile.SaveAs(PathImage);
                    Avatar = Informations.basepath + "Images/Avatar/" + NameImg + fileExt;
                    id_Avatar.Value = "Images/Avatar/" + NameImg + fileExt; ;
                    Img_Upload.ImageUrl = Avatar;
                }
                catch
                {
                    Megs += "alert('Quá trình Upload lỗi!');</script>";
                    Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "canhbao", Megs);
                }
            }
            else
            {
                Megs += "alert('Ảnh chọn phải ở dạng .jpg, .gif, .png, .jpge!');</script>";
                Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "canhbao", Megs);
            }
        }
    }
}